import re
from qturboframe.lib.report.log import logger
from qturboframe.lib.webdriver.exceptions import SeleniumError
from BladeFerm.hui_app.hui_lib.el_tree import ElTree as BladeElTree
from selenium.webdriver import ActionChains


class ElTree(BladeElTree):
    '''操作员中心可拖动的树形控件操作'''

    def drag_value_for_multiple(self, drag_value, drop_value):
        '''
        可拖动树形控件拖动操作

        :Args:
         - drag_value: 需要被拖动的树形结构顺序的节点值，例：'XX1/XX2/XX3'
         - drop_value: 要被拖至的树形结构顺序的节点值，例：'XX1/XX2/XX3'

        :Example:
         - 例: tree.set_value_for_multiple('根节点1/子节点1/叶子节点1', '根节点2/子节点2/叶子节点2')
        '''
        # 先将节点打开，并获取节点对象
        # drag_value = self.__get_tree_label(drag_value).find_element_by_attribute('xpath', '../parent::div[contains(@class,"el-tree-node")]')
        drag_value = self.__get_tree_label(drag_value)
        # drop_value = self.__get_tree_label(drop_value).find_element_by_attribute('xpath', '../parent::div[contains(@class,"el-tree-node")]')
        drop_value = self.__get_tree_label(drop_value)
        action_chains = ActionChains(self._driver)
        # action_chains.context_click(drag_value).perform()
        # action_chains.click_and_hold(drag_value).move_to_element(drop_value).release(drop_value).perform()
        action_chains.drag_and_drop(drag_value, drop_value).perform()

    def __get_tree_label(self, value):
        value_list = value.split('/')
        # 打开树枝选项
        if len(value_list) > 1:
            last_value = value_list[-1]
            del value_list[-1]
            temp_ul = self._element
            for va in value_list:
                if temp_ul is not None:
                    up_div = self.__open_tree_node(temp_ul, va)
                    temp_ul = up_div
        else:
            temp_ul = self._element
            last_value = value_list[0]
        # 选择树叶选项
        if temp_ul is not None:
            logger.info('选择树形节点：' + last_value)
            temp_label = temp_ul.find_element_by_attribute('xpath', './div/div/span[contains(@class,"el-tree-node__label") and text()="{}"]'.format(last_value), timeout=self.timeout)
            return temp_label
        else:
            raise SeleniumError('中间节点未打开！')

    def __open_tree_node(self, ul, node_name):
        re_open = 'expanded'
        title_span = ul.find_element_by_attribute('xpath',
            './div/div/span[contains(@class,"el-tree-node__label") and text()="{}"]'.format(node_name),
            timeout=self.timeout)
        arrow_span = title_span.find_element_by_attribute(
            'xpath', './preceding-sibling::span[contains(@class,"el-tree-node__expand-icon")]', timeout=self.timeout)
        logger.debug("打开树形节点：" + node_name)
        if not re.search(re_open, arrow_span.get_attribute('class')):
            arrow_span.secure_click()
        next_ul = arrow_span.exist('xpath', './parent::div/following-sibling::div', timeout=self.timeout)
        if next_ul:
            if not next_ul.is_displayed():
                next_ul = None
        return next_ul